<%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 2020/7/15
  Time: 18:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<script>
    //防止二次加载失败
    function backHtml() {
        aouDivHtml = jqueryId("aouDiv").html();
    }


    //ajax重新加载数据
    function query() {
        datatable.ajax.reload();
    }

    //批量删除
    function bothDelete() {
        var arr = [];
        $("[name=userIds]:checked").each(function () {
            arr.push($(this).val());
        })
        if (arr.length > 0) {
            bootBoxConfirm("你确定要" + arr.length + "删除吗？", "批量删除品牌",
                function (result) {
                    console.log(result);
                    if (result) {
                        jqueryAjax(
                            "<%=basePath%>user/bothdeleteUser.do",
                            {
                                arr: arr,

                            },
                            function (result) {
                                if (result.code == 0) {
                                    bootBoxAlert(result.msg);
                                    query();//重新加载datatables
                                }
                            }
                        )
                    }


                }
            )
        }

    }

    //删除
    function deleteUser(userId) {
        bootBoxConfirm("你确定要删除吗？", "删除品牌",
            function (result) {
                console.log(result);
                if (result) {
                    jqueryAjax(
                        "<%=basePath%>user/deleteUser.do",
                        {
                            userId: userId,

                        },
                        function (result) {
                            if (result.code == 0) {
                                bootBoxAlert(result.msg);
                                query();//重新加载datatables
                            }
                        }
                    )
                }


            }
        )
    }

    //显示修改页面
    function showUpdate(userId) {
        jqueryAjax("<%=basePath%>/user/toUpdateUser.do", {userId: userId},
            function (result) {
                console.log(result);
                if (result.code == 0) {
                    jqueryName("aouUserId").val(result.data.userId);
                    jqueryName("aouUserName").val(result.data.userName);
                    jqueryName("aouUserPassword").val(result.data.userPassword);
                    jqueryName("aouUserRealName").val(result.data.userRealName);
                    jqueryName("aouUserAge").val(result.data.userAge);
                    jqueryName("aouUserEmail").val(result.data.userEmail);
                    jqueryName("aouUserPhone").val(result.data.userPhone);

                    jqueryName("aouUserSex").each(function () {
                        if ($(this).val() == result.data.userSex) {
                            $(this).prop("checked", true);
                        }
                    });
                    jqueryName("aouUserHot").each(function () {
                        if ($(this).val() == result.data.userIsHot) {
                            $(this).prop("checked", true);
                        }
                    });

                    initFileInput("fileImg", "aouUserLogo", ["/mall/" + result.data.userLogo])

                    bootBoxConfirm(jqueryId("aouDiv form"), "修改品牌",
                        function (type) { //确定才修改
                            if (type) {

                                jqueryAjax(
                                    "<%=basePath%>user/updateUser.do",
                                    {
                                        userId: jqueryName("aouUserId").val(),
                                        userName: jqueryName("aouUserName").val(),
                                        userPassword: jqueryName("aouUserPassword").val(),
                                        userRealName: jqueryName("aouUserRealName").val(),
                                        userAge: jqueryName("aouUserAge").val(),
                                        userSex: jqueryName("aouUserSex", true).val(),
                                        userPhone: jqueryName("aouUserPhone").val(),
                                        userEmail: jqueryName("aouUserEmail").val(),
                                    },
                                    function (result) {
                                        if (result.code == 0) {
                                            bootBoxAlert(result.msg);
                                            query();//重新加载datatables
                                        }
                                    }
                                )


                            }


                        }
                    )


                }


            }
        )
        jqueryId("aouDiv").html(aouDivHtml);//恢复html结构
    }

    //新增
    function showAdd() {
        bootBoxConfirm(
            jqueryId("aouDiv form"),
            "新增品牌",
            function (result) {
                console.log(result);

                if (result) {
                    jqueryAjax(
                        "<%=basePath%>user/addUser.do",
                        {
                            userName: jqueryName("aouUserName").val(),
                            userPassword: jqueryName("aouUserPassword").val(),
                            userRealName: jqueryName("aouUserRealName").val(),
                            userAge: jqueryName("aouUserAge").val(),
                            userSex: jqueryName("aouUserSex", true).val(),
                            userPhone: jqueryName("aouUserPhone").val(),
                            userEmail: jqueryName("aouUserEmail").val(),
                        },
                        function (result) {
                            if (result.code == 0) {
                                bootBoxAlert(result.msg);
                                query();//重新加载datatables
                            }
                        }
                    )
                }


                jqueryId("aouDiv").html(aouDivHtml);//恢复html结构
            }
        )
        initFileInput("fileImg", "aouUserLogo");//初始化上传控件
    }

    //初始化datatables
    function initDatatables(tableId, url, data, columns) {
        return jqueryId(tableId).DataTable({
            //开启服务端模式
            serverSide: true,
            //是否显示加载中
            processing: true,
            //开启是否显示搜索框(我们自己做条件查询)
            searching: false,
            //设置每页显示条数下拉框的值
            lengthMenu: [5, 10, 20],
            //是否开启排序功能
            ordering: true,
            //指定某些列不使用排序
            //columnDefs: [ {orderable: false, targets: [0,1,2]}],
            //隔行换色//根据返回数据,设置某行某列样式
            createdRow: function (row, data, index) {
                //console.log(data);
                // if ( index %2 == 0 ) {
                //     $('td', row).css('font-weight',"bold").css("color","red");
                // }
            },
            language: {//指定国际化资源文件
                "url": "<%=basePath%>js/DataTables/Chinese.json"
            },
            ajax: {
                //设置请求的地址
                "url": url,
                "type": "post",
                "data": data,
                dataSrc: function (result) {//ajax的返回函数,返回值打印控制台,方便打印控制台,进行调试
                    console.log("datatable 返回的数据:");
                    console.log(result);
                    if (result.code == 0)//成功
                    {
                        result.draw = result.data.draw;
                        result.recordsFiltered = result.data.recordsFiltered;
                        result.recordsTotal = result.data.recordsTotal;
                        return result.data.data
                    }
                    return {};
                }
            },
            //设置列
            columns: columns
        });
    }


    //bootbox confirm
    function bootBoxConfirm(message, title, fun) {
        bootbox.confirm({
            message: message,
            size: 'large',
            title: title,
            buttons: {
                confirm: {
                    label: '<span class="glyphicon glyphicon-ok"></span>确定',
                    className: 'btn-success'
                },
                cancel: {
                    label: '<span class="glyphicon glyphicon-remove"></span>取消',
                    className: 'btn-danger'
                }
            },
            callback: fun
        })
    }

</script>
</body>
</html>
